﻿using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialManagementService.Dto;
using Mes.Pendant.UnitMeasures.Dto;
using Mes.Pendant.UnitMeasureW;
using Mes.Pendant.Workstation.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.UnitMeasures
{

	public class UnitMeasuresAppService : CrudAppService<UnitMeasure, UnitMeasureDto, int, PagedResultRequestDto, UnitMeasureDto>, IUnitMeasureService
    {
        private readonly IRepository<UnitMeasure, int> _unitMeasureRepository;
        public UnitMeasuresAppService(IRepository<UnitMeasure, int> repository) : base(repository)
        {
            _unitMeasureRepository = repository;
        }

        /// <summary>
        /// 获取计量单位列表
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="customerName"></param>
        /// <param name="isEffective"></param>
        /// <returns></returns>
        public (List<UnitMeasureDto> list, int total) GetCustomerList(string? customerCode, string? customerName, int isEffective, int page = 1, int limit = 10)
        {
            var query = _unitMeasureRepository.GetListAsync().Result.AsQueryable().WhereIf(!string.IsNullOrEmpty(customerCode), c => c.UnitMeasureCode.Contains(customerCode)).WhereIf(!string.IsNullOrEmpty(customerName), c => c.UnitMeasureName.Contains(customerName));

             //var query = _unitMeasureRepository.GetListAsync().Result.AsQueryable();

            var total = query.Count();
            query = query.Skip((page - 1) * limit).Take(limit);
            var settings = query.Select(x => new UnitMeasureDto
            {
                Id = x.Id,
                UnitMeasureCode = x.UnitMeasureCode,
                UnitMeasureName = x.UnitMeasureName,
                IsPrimaryUnit = x.IsPrimaryUnit,
                ConversionScale = x.ConversionScale,
                State = x.State,
            }).ToList();
            return (settings, total);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="unitMeasure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CreatUnitMeasureInfo(UnitMeasureDto unitMeasure)
        {
            var unitIsExist = _unitMeasureRepository.AnyAsync(m => m.UnitMeasureCode == unitMeasure.UnitMeasureCode).Result;
            if (unitIsExist)
            {
                return -1;
            }

            var unitModel = AutoMapperExt.MapTo<UnitMeasure>(unitMeasure);
            await _unitMeasureRepository.InsertAsync(unitModel);
            return unitMeasure.Id;

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="unitMeasure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateUnitMeasureInfos(UnitMeasureDto unitMeasure)
        {
            var unitIsExist = _unitMeasureRepository.AnyAsync(m => m.UnitMeasureCode == unitMeasure.UnitMeasureCode).Result;
            if (unitIsExist)
            {
                return -1;
            }

            var unitModel = AutoMapperExt.MapTo<UnitMeasure>(unitMeasure);
            await _unitMeasureRepository.UpdateAsync(unitModel);
            return unitMeasure.Id;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="unitMeasureId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteUnitMeasureInfo(int id)
        {
            _unitMeasureRepository.DeleteAsync(id).Wait();
            return id;
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UnitMeasureDto> GetUnitMeasureById(int id)
        {

            UnitMeasure uniEntity = _unitMeasureRepository.GetAsync(id).Result;
            UnitMeasureDto unitMeasure = ObjectMapper.Map<UnitMeasure, UnitMeasureDto>(uniEntity);
            return unitMeasure;
        }

        /// <summary>
        /// 获取主计量单位
        /// </summary>
        /// <param name="isMainUnit"></param>
        /// <returns></returns>
        public List<UnitMeasureDto> GetMainUnits(int isMainUnit = 1)
        {
            var entities = _unitMeasureRepository.GetListAsync().Result;
            var mainUnits = entities.Where(x => x.ConversionScale == isMainUnit).Select(x => new UnitMeasureDto
            {
                Id = x.Id,
                UnitMeasureCode = x.UnitMeasureCode,
                UnitMeasureName = x.UnitMeasureName,
                IsPrimaryUnit = x.IsPrimaryUnit,
                ConversionScale = (int)x.ConversionScale,
                State = x.State,
            }).ToList();
            return mainUnits;
        }

    }

}
